﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolLibrary.Entity;
using SchoolLibrary.Utils;
using SchoolLibrary.DataBase.DAO;
using SchoolLibrary.Entity.Person;

namespace SchoolLibrary.Desktop.Guide.Input
{
    public partial class BookInputDialog : Form
    {
        private const int YEAR_MAX_LENGHT = 4;

        private Book book;

        public BookInputDialog(Book book) : this()
        {
            this.book = book;
            this.nameBox.Text = book.Name;
            this.yearBox.Text = book.Year.ToString();
            this.priceBox.Text = book.Price.ToString();
            this.countBox.Text = book.Amount.ToString();
            setGenre(book.Genre);

            foreach (Writer writer in book.Writers)
            {
                this.writersListBox.Items.Add(writer);
            }

            validate();
        }

        private void setGenre(Genre genre)
        {
            foreach (object obj in genreComboBox.Items)
            {
                Genre currentGenre = (Genre)obj;
                if (currentGenre.Name.Equals(genre.Name))
                {
                    this.genreComboBox.SelectedItem = currentGenre;
                }
            }
        }

        public BookInputDialog()
        {
            InitializeComponent();
            initValidators();
            initData();
        }

        private void initValidators()
        {
            this.yearBox.MaxLength = YEAR_MAX_LENGHT;
            this.yearBox.TextChanged += new System.EventHandler(this.validate);
            this.nameBox.TextChanged += new System.EventHandler(this.validate);
            this.priceBox.TextChanged += new System.EventHandler(this.validate);
        }

        private void initData()
        {
            List<Genre> genries = GenreDAO.getInstance().findAll();
            foreach (Genre genre in genries)
            {
                this.genreComboBox.Items.Add(genre);
            }
            if (this.genreComboBox.Items.Count > 0)
            {
                this.genreComboBox.SelectedIndex = 0;
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (book == null)
            {
                book = new Book();
            }
            book.Name = this.nameBox.Text;
            book.Price = Int32.Parse(this.priceBox.Text);
            book.Year = Int32.Parse(this.yearBox.Text);
            book.Amount = Int32.Parse(this.countBox.Text);
            book.Genre = (Genre) genreComboBox.SelectedItem;
            Writer[] writers = new Writer[this.writersListBox.Items.Count];
            this.writersListBox.Items.CopyTo(writers, 0);
            book.Writers = new List<Writer>(writers);
            this.Close();
        }

        private void validate(object sender, EventArgs e)
        {
            validate();
        }

        private void validate()
        {
            bool isValid = !StringUtil.isEmptyString(this.nameBox.Text) &&
                !StringUtil.isEmptyString(this.priceBox.Text) &&
                !StringUtil.isEmptyString(this.yearBox.Text) &&
                !StringUtil.isEmptyString(this.countBox.Text) &&
                this.writersListBox.Items.Count > 0;
            this.okButton.Enabled = isValid;
        }

        private void addWriterButton_Click(object sender, EventArgs e)
        {
            WriterGuideForm form = new WriterGuideForm(ViewModeEnum.SELECT);
            form.initData();
            form.ShowDialog();
            Writer writer = form.SelectedObject;
            if (writer != null)
            {
                writersListBox.Items.Add(writer);
            }
            validate();
        }

        private void removeWriterButton_Click(object sender, EventArgs e)
        {
           
        }

        private void writersListBox_SelectedValueChanged(object sender, EventArgs e)
        {
            this.removeWriterButton.Enabled = this.writersListBox.Items.Count > 0;
        }

        public Book Book
        {
            get { return book; }
        }
    }
}
